package com.example.loginpage

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Switch
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.example.loginpage.CloudSQL.MySQLConnection
import com.example.loginpage.InputUtil.DataViewModel
import com.google.android.material.bottomnavigation.BottomNavigationView
import java.lang.Exception
import java.lang.reflect.Method

const val Activity_Account = "account"
class MainActivity : AppCompatActivity() {
    private lateinit var dataViewModel: DataViewModel
    private lateinit var mySQL: MySQLConnection
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        dataViewModel = ViewModelProvider(this)[DataViewModel::class.java]
        mySQL = ViewModelProvider(this)[MySQLConnection::class.java]

        //关联NavController与BottomNavigationView
        val navHost = supportFragmentManager.findFragmentById(R.id.nav_home)
                as NavHostFragment
        val navController = navHost.navController
        val bottomNavigation: BottomNavigationView = findViewById(R.id.bottomNavigation)
        bottomNavigation.setupWithNavController(navController)
        bottomNavigation.setOnItemSelectedListener {
            when(it.itemId){
                R.id.ChatFragment-> {}
                R.id.StoreFragment-> {}
                R.id.SettingFragment-> {}
                else-> Toast.makeText(this, "导航未知", Toast.LENGTH_LONG).show()
            }
            //完成导航功能
            navController.navigate(it.itemId)
            true
        }
    }
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)
        setIconEnable(menu!!,true)
        menuInflater.inflate(R.menu.menu_user,menu)
        // 是否开启语音播报
        val switchItem = menu.findItem(R.id.ttsSwitch)
        switchItem.actionView?.findViewById<Switch>(R.id.Switch_item)
            ?.setOnCheckedChangeListener { _, isChecked ->
                dataViewModel.setTTS((isChecked))
                if (isChecked) {
                    Toast.makeText(this, "开启语音播报", Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(this, "关闭语音播报", Toast.LENGTH_LONG).show()
                }
            }
        return true
    }

    //设置菜单始终可见
    private fun setIconEnable(menu: Menu, enable:Boolean){
        try{
            val clazz=Class.forName("androidx.appcompat.view.menu.MenuBuilder")
            val m: Method = clazz.getDeclaredMethod("setOptionalIconVisible",Boolean::class.javaObjectType)
            m.isAccessible = true
            m.invoke(menu,enable)
        }catch (e: Exception){
            e.printStackTrace()
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        super.onOptionsItemSelected(item)
        val navHost = supportFragmentManager.findFragmentById(R.id.nav_home)
                as NavHostFragment
        val navController = navHost.navController
        when(item.itemId){
            R.id.UserFragment-> {}
            R.id.OthersFragment-> {}
            else-> Toast.makeText(this, "导航未知", Toast.LENGTH_LONG).show()
        }
        navController.navigate(item.itemId)
        return true
    }

}